package com.koron.common.core.config;

import com.koron.bean.app.AppBean;
import com.koron.bean.app.AppSimpleBean;
import com.koron.bean.system.staff.vo.EamUser;
import com.koron.common.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName ThreadLocalContext
 * @description
 * @Author zhouj
 * @Date 2021/7/15 14:37
 */
@Slf4j
public class ThreadLocalContext {

    private static final ThreadLocal<EamUser> userContextHolder = new CustomInheritableThreadLocal<EamUser>();

    private static final ThreadLocal<AppBean> appContextHolder = new CustomInheritableThreadLocal<AppBean>();

    public static void set(EamUser user) {
        userContextHolder.set(user);
    }

    /*
     * 功能描述
     * @author zhouj
     * @date 2021/10/25 8:55
     * @param []
     * @description 当前用户信息
     * @return com.koron.bean.system.staff.vo.EamUser
     */
    public static EamUser get() {
        if (userContextHolder.get() == null) {
            EamUser eamUser = new EamUser();
            set(eamUser);
            return eamUser;
        }
        return userContextHolder.get();
    }

    /*
     * 功能描述
     * @author zhouj
     * @date 2021/10/25 8:55
     * @param []
     * @description 当前应用信息
     * @return com.koron.bean.system.staff.vo.EamUser
     */
    public static AppBean getApp() {
        if (appContextHolder.get() == null) {
            EamUser eamUser = ThreadLocalContext.get();
            if (Objects.isNull(eamUser)) {
                return new AppBean();
            }
            String currDs = ThreadLocalContext.get().getCurrDs();

            log.info("ThreadLocal打印当前水司:{}", currDs);
            List<AppSimpleBean> appSimpleBeanList = eamUser.getAppSimpleBeanList();
            if (CollectionUtils.isEmpty(appSimpleBeanList) && StringUtils.isEmpty(currDs)) {
                return new AppBean();
            }
            if (CollectionUtils.isEmpty(appSimpleBeanList)) {
                AppBean appBean = new AppBean();
                appBean.setMark(currDs);
                return appBean;
            }
            for (AppSimpleBean appSimpleBean : appSimpleBeanList) {
                if (StringUtils.equals(currDs, appSimpleBean.getAppMark())) {
                    AppBean appBeanTemp = new AppBean();
                    appBeanTemp.setId(appSimpleBean.getAppId());
                    appBeanTemp.setCode(appSimpleBean.getAppCode());
                    appBeanTemp.setMark(appSimpleBean.getAppMark());
                    appBeanTemp.setName(appSimpleBean.getAppName());
                    appBeanTemp.setSign(appSimpleBean.getSign());
                    appBeanTemp.setStatus(appSimpleBean.getStatus());
                    setApp(appBeanTemp);
                    break;
                }
            }
        }
        return appContextHolder.get();
    }

    public static void setApp(AppBean appBean) {
        appContextHolder.set(appBean);
    }

    public static void clear() {
        //清理ThreadLocal信息
        userContextHolder.remove();
        appContextHolder.remove();
    }

    public static EamUser getLoginUser() {
        //区分是否为代理模式
        EamUser eamUser = ThreadLocalContext.get();
        EamUser agent = eamUser.getAgent();
        if (Objects.isNull(agent)) {
            return eamUser;
        }
        return agent;
    }

}
